package cn.binux.portal.controller;

import cn.binux.constant.Const;
import cn.binux.pojo.TbUser;
import cn.binux.portal.service.PortalContentService;
import cn.binux.redis.service.JedisClient;
import cn.binux.utils.CookieUtils;
import cn.binux.utils.FastJsonConvert;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang3.StringUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * Created by cynicism on 17/1/27.
 */
@Controller
public class IndexController {

    @Reference(version = Const.XBIN_STORE_PORTAL_VERSION)
    private PortalContentService portalContentService;

	@Reference(version = Const.XBIN_STORE_REDIS_VERSION)
	private JedisClient jedisClient;

    @Value("${big_ad_index}")
    private long Big_AD_INDEX;

	@Value("${redisKey.prefix.user_session}")
	private String USER_SESSION;

    @RequestMapping("/index")
    public String index(HttpServletRequest request, HttpServletResponse response,Model model) {
		String cookieUUID = CookieUtils.getCookieValue(request, Const.CART_KEY);
		String tokenLogin = CookieUtils.getCookieValue(request, Const.TOKEN_LOGIN);

		TbUser user = null;
		String userJson = null;
		if (StringUtils.isNoneEmpty(tokenLogin)) {
			try {
				userJson = jedisClient.get(USER_SESSION + tokenLogin);
			} catch (Exception e) {
//				logger.error("Redis 错误", e);
			}
			if (StringUtils.isNoneEmpty(userJson)) {
				user = FastJsonConvert.convertJSONToObject(userJson, TbUser.class);
			}
			model.addAttribute("user", user);
		} else {
			model.addAttribute("user", user);
		}

        return "index";
    }
    @RequestMapping("/index2")
    public String index2() {

        return "index";
    }
    @RequestMapping("/sideBar/popupLogin")
    public String popupLogin() {

        return "popupLogin";
    }


    //[LString;{"buttonPositionTop":false,"buttonPositionLeft":false,"largeIcon":true,"assistantFirstStart":false,"showWarnings":true,"scriptVersion":1}
    @RequestMapping("/adguard-ajax-api/api")
    @ResponseBody
    public String adguard() {

        return "[LString;{\"buttonPositionTop\":false,\"buttonPositionLeft\":false,\"largeIcon\":true,\"assistantFirstStart\":false,\"showWarnings\":true,\"scriptVersion\":1}";
    }
}
